<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
var name = 'windowName'

const obj = {
    name: 'objName',
    getName: function (a, b) {
        console.log(this.name, a, b)
        return a + b
    }
}
const obj2 = { name: 'obj2Name', }

Function.prototype.call2 = function (context) {
    // 获取取 context 之后的参数
    const args = []

    for (let i = 1; i < arguments.length; i++) {
        args.push(arguments[i])
    }

    function getContext(target) {
        return (target === null || target === undefined) ? window : Object(target)
    }

    context = getContext(context)
    context.fn = this
    const result = context.fn(...args)
    // 执行完毕删除 fn
    delete context.fn
    // 用于返回函数的返回值
    return result
}
const num = obj.getName.call2(obj2, 1, 2)   // obj2Name 1 2
console.log(num)                            // 3
obj.getName.call2(null, 1, 2)               // windowName 1 2
obj.getName.call2(undefined, 1, 2)          // windowName 1 2
obj.getName.call2(true, 1, 2)               // undefined 1 2
</script>
</body>
</html>
